package com.rong.creatmodel.singlemodel.example;

import java.io.Serializable;

// 需要序列化写入的对象
public class Single implements Serializable {

    private static boolean flage = false;

    private Single(){
        synchronized (Single.class){
            if (flage){
                throw new RuntimeException("单例模式不能创建多个对对象");
            }else {
                flage = true;
            }
        }

    }

    private static class SingleHolder{

        private static final Single INSTANCE = new Single();
    }

    public static Single getInstance(){
        return SingleHolder.INSTANCE;
    }

    // 当进行反序列化时，会自动调用该方法 将该方法的返回值直接返回
    public Object readResolve(){
        return SingleHolder.INSTANCE;
    }
}
